{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出9x9乘法口诀表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "def multiplication_table():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i * j}\\t\", end='')\n",
    "        print()\n",
    "\n",
    "choice = input(\"请输入1来运行乘法口诀表程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    multiplication_table()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8 = 2*2*2"
     ]
    }
   ],
   "source": [
    "def factorize():\n",
    "    num = int(input(\"请输入一个正整数: \"))\n",
    "    print(num, \"=\", end=\" \")\n",
    "    factor = 2\n",
    "    while num > 1:\n",
    "        while num % factor == 0:\n",
    "            print(factor, end=\"\")\n",
    "            num //= factor\n",
    "            if num > 1:\n",
    "                print(\"*\", end=\"\")\n",
    "            else:\n",
    "                break\n",
    "        factor += 1\n",
    "\n",
    "choice = input(\"请输入1来运行分解质因数程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    factorize()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数字是2位数\n",
      "逆序打印各位数字: 22"
     ]
    }
   ],
   "source": [
    "def digit_operation():\n",
    "    num_str = input(\"请输入一个不多于6位的正整数: \")\n",
    "    num = int(num_str)\n",
    "    digit_count = len(num_str)\n",
    "    print(f\"该数字是{digit_count}位数\")\n",
    "    print(\"逆序打印各位数字: \", end=\"\")\n",
    "    while num > 0:\n",
    "        print(num % 10, end=\"\")\n",
    "        num //= 10\n",
    "\n",
    "choice = input(\"请输入1来运行数字操作程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    digit_operation()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算结果为：37035\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def sum_series():\n",
    "    a = input(\"请输入一个数字（0 - 9之间）：\")\n",
    "    n = input(\"请输入相加的项数：\")\n",
    "    a = int(a)\n",
    "    n = int(n)\n",
    "    sum_value = 0\n",
    "    num = 0\n",
    "    for i in range(n):\n",
    "        num = num * 10 + a\n",
    "        sum_value += num\n",
    "    print(f\"计算结果为：{sum_value}\")\n",
    "    \n",
    "choice = input(\"请输入1来运行求和程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    sum_series()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数列前5项之和为：8.391666666666667\n"
     ]
    }
   ],
   "source": [
    "def sum_fraction_sequence():\n",
    "    n = int(input(\"请输入项数N（N为正整数）：\"))\n",
    "    sum_value = 0\n",
    "    numerator = 2  \n",
    "    denominator = 1  \n",
    "    for _ in range(n):\n",
    "        sum_value += numerator / denominator\n",
    "        new_numerator = numerator + denominator\n",
    "        denominator = numerator\n",
    "        numerator = new_numerator\n",
    "    print(f\"该数列前{n}项之和为：{sum_value}\")\n",
    "    \n",
    "choice = input(\"请输入1来运行分数序列求和程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    sum_fraction_sequence()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "符合条件的整数是：4256\n"
     ]
    }
   ],
   "source": [
    "def find_number():\n",
    "    result = []\n",
    "    for i in range(1, 100000):\n",
    "        n = i + 100\n",
    "        m = n + 268\n",
    "        n_sqrt = int(n ** 0.5)\n",
    "        m_sqrt = int(m ** 0.5)\n",
    "        if n_sqrt ** 2 == n and m_sqrt ** 2 == m:\n",
    "            result.append(i)\n",
    "    for num in result:\n",
    "        print(f\"符合条件的整数是：{num}\")\n",
    "    \n",
    "choice = input(\"请输入1来运行查找该整数的程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    find_number()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第6个月兔子的总数是8对。\n"
     ]
    }
   ],
   "source": [
    "def rabbit_count():\n",
    "    month = int(input(\"请输入要查看的月数（正整数）：\"))\n",
    "    if month == 1 or month == 2:\n",
    "        print(f\"第{month}个月兔子的总数是1对。\")\n",
    "    else:\n",
    "        a, b = 1, 1\n",
    "        for _ in range(month - 2):\n",
    "            a, b = b, a + b\n",
    "        print(f\"第{month}个月兔子的总数是{b}对。\")\n",
    "\n",
    "choice = input(\"请输入1来运行兔子数量计算程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    rabbit_count()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请按行输入3x3矩阵的元素（每行3个数字，用空格隔开）：\n",
      "该3x3矩阵对角线元素之和为：25\n"
     ]
    }
   ],
   "source": [
    "def matrix_diagonal_sum():\n",
    "    matrix = []\n",
    "    print(\"请按行输入3x3矩阵的元素（每行3个数字，用空格隔开）：\")\n",
    "    for _ in range(3):\n",
    "        row = list(map(int, input().split()))\n",
    "        matrix.append(row)\n",
    "    sum_value = 0\n",
    "    for i in range(3):\n",
    "        sum_value += matrix[i][i]\n",
    "        sum_value += matrix[i][2 - i]\n",
    "    sum_value -= matrix[1][1]  \n",
    "    print(f\"该3x3矩阵对角线元素之和为：{sum_value}\")\n",
    "\n",
    "choice = input(\"请输入1来运行求矩阵对角线元素之和程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    matrix_diagonal_sum()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请依次输入这n个整数，用空格隔开：\n",
      "移动后的整数序列为： [3, 4, 6]\n"
     ]
    }
   ],
   "source": [
    "def shift_numbers():\n",
    "    n = input(\"请输入整数的个数n（正整数）：\")\n",
    "    m = input(\"请输入向后移的位置个数m（正整数且m小于等于n）：\")\n",
    "    n = int(n)\n",
    "    m = int(m)\n",
    "    num_list = []\n",
    "    print(\"请依次输入这n个整数，用空格隔开：\")\n",
    "    num_list = list(map(int, input().split()))\n",
    "    shifted_list = num_list[-m:] + num_list[:-m]\n",
    "    print(\"移动后的整数序列为：\", shifted_list)\n",
    "    \n",
    "\n",
    "choice = input(\"请输入1来运行整数位置移动程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    shift_numbers()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算结果为：1.5333333333333332\n"
     ]
    }
   ],
   "source": [
    "def sum_series():\n",
    "    n = int(input(\"请输入一个整数n：\"))\n",
    "    result = 0\n",
    "    if n % 2 == 0:\n",
    "        for i in range(2, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    else:\n",
    "        for i in range(1, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    print(f\"计算结果为：{result}\")\n",
    "    \n",
    "\n",
    "choice = input(\"请输入1来运行求和程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    sum_series()\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此题第二种解法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算结果为：0.9166666666666666\n"
     ]
    }
   ],
   "source": [
    "def sum_even_sequence(n):\n",
    "    \"\"\"\n",
    "    计算输入为偶数n时，1/2 + 1/4 +... + 1/n的和\n",
    "    \"\"\"\n",
    "    sum_result = 0\n",
    "    for num in range(2, n + 1, 2):\n",
    "        sum_result += 1 / num\n",
    "    return sum_result\n",
    "\n",
    "\n",
    "def sum_odd_sequence(n):\n",
    "    \"\"\"\n",
    "    计算输入为奇数n时，1/1 + 1/3 +... + 1/n的和\n",
    "    \"\"\"\n",
    "    sum_result = 0\n",
    "    for num in range(1, n + 1, 2):\n",
    "        sum_result += 1 / num\n",
    "    return sum_result\n",
    "def switch(value):\n",
    "    n = int(input(\"请输入一个整数n：\"))\n",
    "    if value == \"even\":\n",
    "        result = sum_even_sequence(n)\n",
    "        print(f\"计算结果为：{result}\")\n",
    "    elif value == \"odd\":\n",
    "        result = sum_odd_sequence(n)\n",
    "        print(f\"计算结果为：{result}\")\n",
    "    else:\n",
    "        print(\"你输入的参数不合法，请输入'even'或者'odd'\")\n",
    "        choice = input(\"请输入1来运行求和程序（输入其他内容则退出）：\")\n",
    "if choice == \"1\":\n",
    "    option = input(\"请输入'even'或者'odd'：\")\n",
    "    switch(option)\n",
    "else:\n",
    "    print(\"你输入的内容不符合要求，程序结束。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
